//
//  ProjectApi.m
//  Project
//
//  Created by 冯东辉 on 2018/9/11.
//  Copyright © 2018年 FengDongHui. All rights reserved.
//

#import "ProjectApi.h"
#import "DataService.h"

#define DefauleTimeOut 30

@implementation ProjectApi

-(id)init {
    
    if (self = [super init]) {
        self.dataService = [DataService sharedInstance];
    }
    return self;
}

/**
 单例
 
 @return 返回唯一实例
 */
+ (ProjectApi* )instance {
    
    static ProjectApi *sharedSingleton;
    
    @synchronized(self) {
        
        if (!sharedSingleton){
            sharedSingleton = [[ProjectApi alloc] init];
        }
        return sharedSingleton;
    }
}

/**
 登录
 
 @param userId 用户手机号
 @param result 返回Model
 */
- (void)loginWithUserId:(NSString *)userId
               callBack:(void(^)(NetModel *  object))result {
    
    NSDictionary *params = @{@"userId":userId};
    
    [self.dataService postDataByMethods:params
                      withCacheDuration:0
                                timeout:DefauleTimeOut
                              urlMehtod:[NSString stringWithFormat:@"%@/apploginapi/postloginpasswordverify", BASE_URL]
                               callBack:^(NetModel *object) {
                                   result(object);
                               }];
}

/**
 首页维度指标和劣化个数
 
 @param cityName 城市
 @param result 返回Model
 */
- (void)getModuleDataWithCityName:(NSString *)cityName
                         callBack:(void(^)(NetModel *  object))result {
    
    NSDictionary *params = @{@"pnamekey":@"getdimcountdata",
                             @"cityName":cityName};
    
    [self.dataService getDataByMethods:params
                     withCacheDuration:0
                               timeout:DefauleTimeOut
                             urlMehtod:[NSString stringWithFormat:@"%@/apppsiapi/getkey", BASE_URL]
                              callBack:^(NetModel *object) {
                                  result(object);
                              }];
}

/**
 获取用户ID拥有的地市权限
 
 @param result 返回Model
 */
- (void)getUserCityDataCallBack:(void(^)(NetModel *  object))result {
    
    NSString *userId = [[NSUserDefaults standardUserDefaults] objectForKey:USER_PHONE_NUM];
    
    if ([ProjectTools isValiString:userId]) {
        
        userId = @"";
    }
    
    [self.dataService getDataByMethods:nil
                     withCacheDuration:0
                               timeout:DefauleTimeOut
                             urlMehtod:[NSString stringWithFormat:@"%@/apppsiapi/getkeywhere?id=getusercity_data&USER_ID=%@", BASE_URL, userId]
                              callBack:^(NetModel *object) {
                                  result(object);
                              }];
}

/**
 维度信息点击获取列表
 
 @param cityName 城市
 @param type 判断需要数据类型（所有数据、收藏、短板数据） 1、2、3
 @param sdim_ID ID
 @param page 页数
 @param result 返回Model
 */
- (void)getModuleListDataWithCityName:(NSString *)cityName
                                 type:(NSString *)type
                              sdim_ID:(NSString *)sdim_ID
                                 page:(NSString *)page
                             callBack:(void(^)(NetModel *  object))result {
    
    NSString *userId = [[NSUserDefaults standardUserDefaults] objectForKey:USER_PHONE_NUM];
    
    if ([ProjectTools isValiString:userId]) {
        
        userId = @"";
    }
    
    NSDictionary *params = @{@"id":@"getdimparameter_data",
                             @"CITY_NAME":cityName,
                             @"type":type,
                             @"phone":userId,
                             @"dimid":sdim_ID,
                             @"page":page
                             };
    
    [self.dataService getDataByMethods:params
                     withCacheDuration:0
                               timeout:DefauleTimeOut
                             urlMehtod:[NSString stringWithFormat:@"%@/apppsiapi/getkeywheredim", BASE_URL]
                              callBack:^(NetModel *object) {
                                  result(object);
                              }];
}

/**
 获取实时列表数据
 
 @param cityName 城市
 @param type 判断需要数据类型（所有数据、收藏、短板数据） 1、2、3
 @param h 时间段
 @param page 页数
 @param result 返回Model
 */
- (void)getSsModuleListDataWithCityName:(NSString *)cityName
                                   type:(NSString *)type
                                      h:(NSString *)h
                                   page:(NSString *)page
                               callBack:(void(^)(NetModel *  object))result {
    
    NSString *userId = [[NSUserDefaults standardUserDefaults] objectForKey:USER_PHONE_NUM];
    
    if ([ProjectTools isValiString:userId]) {
        
        userId = @"";
    }
    
    NSDictionary *params = @{@"id":@"getminparameter_data",
                             @"CITY_NAME":cityName,
                             @"type":type,
                             @"phone":userId,
                             @"H":h,
                             @"page":page
                             };
    
    [self.dataService getDataByMethods:params
                     withCacheDuration:0
                               timeout:DefauleTimeOut
                             urlMehtod:[NSString stringWithFormat:@"%@/apppsiapi/GetKeyWhereMin", BASE_URL]
                              callBack:^(NetModel *object) {
                                  result(object);
                              }];
}

/**
 获取短板指标数据
 
 @param cityName 城市
 @param result 返回Model
 */
- (void)getDbModuleListDataWithCityName:(NSString *)cityName
                               callBack:(void(^)(NetModel *  object))result {
    
    NSString *userId = [[NSUserDefaults standardUserDefaults] objectForKey:USER_PHONE_NUM];
    
    if ([ProjectTools isValiString:userId]) {
        
        userId = @"";
    }
    
    NSDictionary *params = @{@"id":@"getshortindicator",
                             @"cityName":cityName,
                             @"USER_ID":userId
                             };
    
    [self.dataService getDataByMethods:params
                     withCacheDuration:0
                               timeout:DefauleTimeOut
                             urlMehtod:[NSString stringWithFormat:@"%@/apppsiapi/getkeywheregroupby", BASE_URL]
                              callBack:^(NetModel *object) {
                                  result(object);
                              }];
}

/**
 收藏操作
 
 @param flag 1收藏 2取消收藏
 @param dimid 指标id
 @param incatorname 指标名称
 @param result 返回Model
 */
- (void)collectHandleWithFlag:(NSString *)flag
                        dimid:(NSString *)dimid
                  incatorname:(NSString *)incatorname
                     callBack:(void(^)(NetModel *  object))result {
    
    NSString *userId = [[NSUserDefaults standardUserDefaults] objectForKey:USER_PHONE_NUM];
    
    if ([ProjectTools isValiString:userId]) {
        
        userId = @"";
    }
    
    NSDictionary *params = @{@"userid":userId,
                             @"flag":flag,
                             @"dimid":dimid,
                             @"incatorname":incatorname
                             };
    
    [self.dataService postDataByMethods:params
                      withCacheDuration:0
                                timeout:DefauleTimeOut
                              urlMehtod:[NSString stringWithFormat:@"%@/apppsiapi/postinsertinticatorfav", BASE_URL]
                               callBack:^(NetModel *object) {
                                   result(object);
                               }];
}

/**
 获取用户的收藏列表
 
 @param cityname 城市
 @param page 页码
 @param result 返回Model
 */
- (void)getCollectListDataWithCityname:(NSString *)cityname
                                  page:(NSString *)page
                              callBack:(void(^)(NetModel *  object))result {
    
    NSString *userId = [[NSUserDefaults standardUserDefaults] objectForKey:USER_PHONE_NUM];
    
    if ([ProjectTools isValiString:userId]) {
        
        userId = @"";
    }
    
    NSDictionary *params = @{@"cityname":cityname,
                             @"phone":userId,
                             @"page":page
                             };
    
    [self.dataService getDataByMethods:params
                     withCacheDuration:0
                               timeout:DefauleTimeOut
                             urlMehtod:[NSString stringWithFormat:@"%@/apppsiapi/getkeywherefavgroupby", BASE_URL]
                              callBack:^(NetModel *object) {
                                  result(object);
                              }];   
}

/**
 获取指标详情
 
 @param sid sid
 @param result 返回Model
 */
- (void)getModuleDetailDataWithSid:(NSString *)sid
                          callBack:(void(^)(NetModel *  object))result {
    
    NSString *userId = [[NSUserDefaults standardUserDefaults] objectForKey:USER_PHONE_NUM];
    
    if ([ProjectTools isValiString:userId]) {
        
        userId = @"";
    }
    
    NSDictionary *params = @{@"sid":sid,
                             @"USER_ID":userId};
    
    [self.dataService getDataByMethods:params
                     withCacheDuration:0
                               timeout:DefauleTimeOut
                             urlMehtod:[NSString stringWithFormat:@"%@/apppsiapi/getwinticatordetail", BASE_URL]
                              callBack:^(NetModel *object) {
                                  result(object);
                              }];
}

/**
 搜索信息列表
 
 @param cityname 城市名称
 @param keyword 关键词
 @param page 页数
 @param result 返回Model
 */
- (void)getSearchInfoDataListWithCityName:(NSString *)cityname
                                  keyWord:(NSString *)keyword
                                     page:(NSString *)page
                                 callBack:(void(^)(NetModel *  object))result {
    
    
    NSDictionary *params = @{@"cityname":cityname,
                             @"keyword":keyword,
                             @"page":page};
    
    [self.dataService getDataByMethods:params
                     withCacheDuration:0
                               timeout:DefauleTimeOut
                             urlMehtod:[NSString stringWithFormat:@"%@/apppsiapi/GetKeyWhereSearchgroupby", BASE_URL]
                              callBack:^(NetModel *object) {
                                  result(object);
                              }];
}

/**
 版本检测
 
 @param result 返回Model
 */
- (void)appUpdateIpaCallBack:(void(^)(NetModel *  object))result {
    
    [self.dataService getDataByMethods:nil
                     withCacheDuration:0
                               timeout:DefauleTimeOut
                             urlMehtod:[NSString stringWithFormat:@"%@/apppsiapi/getisoversionwhere", BASE_URL]
                              callBack:^(NetModel *object) {
                                  result(object);
                              }];
}

@end
